unit UTarifas;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, DBCtrls, Mask, ExtCtrls, RxGIF, RXCtrls, Grids, DBGrids,
  Buttons;

type
  TFrmTarifas = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    DBEdit5: TDBEdit;
    DBComboBox1: TDBComboBox;
    SpeedButton1: TSpeedButton;
    btnRetornar: TSpeedButton;
    DBGrid1: TDBGrid;
    GroupBox2: TGroupBox;
    Edit1: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Bevel1: TBevel;
    Label9: TLabel;
    Label10: TLabel;
    SpeedButton2: TSpeedButton;
    GroupBox3: TGroupBox;
    MaskEdit1: TMaskEdit;
    Label11: TLabel;
    SpeedButton3: TSpeedButton;
    Label12: TLabel;
    Label13: TLabel;
    btnGravar: TSpeedButton;
    btnExcluir: TSpeedButton;
    btnAdicionar: TSpeedButton;
    Bevel2: TBevel;
    Bevel3: TBevel;
    Bevel4: TBevel;
    Bevel5: TBevel;
    Bevel6: TBevel;
    Bevel7: TBevel;
    DBRadioGroup1: TDBRadioGroup;
    Bevel8: TBevel;
    RxLabel1: TRxLabel;
    Image1: TImage;
    Image2: TImage;
    Label14: TLabel;
    Bevel9: TBevel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure btnGravarClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure btnRetornarClick(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure btnAdicionarClick(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmTarifas: TFrmTarifas;

implementation

uses Modulo, funcaos;

{$R *.DFM}

procedure TFrmTarifas.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   Action := CaFree;
end;

procedure TFrmTarifas.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
   if MessageDlg('Deseja encerrar o cadastro de tarifas?',MtConfirmation, [mbyes, mbno],0) = mrno then
      Canclose := false;
end;

procedure TFrmTarifas.btnGravarClick(Sender: TObject);
const
   dias : array[1..7] of string[15] =
   ('Domingo','Segunda-Feira','Terça-Feira','Quarta-Feira','Quinta-Feira','Sexta-Feira','Sábado');
var
   i : integer;
   a, b, c, d, x, horai, horaf, diabusca : string;
begin
   x := DBComboBox1.Text;
   horai := DBEdit2.Text;
   horaf := DBEdit3.Text;
   DMEscola.Qgravatarifa.Close;
   DMEscola.Qgravatarifa.SQL.Clear;
   DMEscola.Qgravatarifa.Sql.Add ('Select * From Tarifas');
   DMEscola.Qgravatarifa.Sql.Add('WHERE ');
   DMEscola.Qgravatarifa.Sql.Add('(:phora >= Horaini)  and');
   DMEscola.Qgravatarifa.Sql.Add('(:phora <= Horafim)  and');
   DMEscola.Qgravatarifa.Sql.Add('(DiaSemana = :pdiasemana) or');
   DMEscola.Qgravatarifa.Sql.Add('(:phoras <= Horafim)  and');
   DMEscola.Qgravatarifa.Sql.Add('(:phoras >= Horaini)  and');
   DMEscola.Qgravatarifa.Sql.Add('(DiaSemana = :pdiasemana)');
   DMEscola.Qgravatarifa.ParamByName('pdiasemana').AsString := x;
   DMEscola.Qgravatarifa.ParamByName('phora').AsTime := StrToTime(horai);
   DMEscola.Qgravatarifa.ParamByName('phoras').AsTime := StrToTime(horaf);
   DMEscola.Qgravatarifa.open;
   diaBusca:=DMEscola.QgravatarifaCobrar.AsString;
   if diabusca = '' then
   begin
      if DBComboBox1.Text = 'Todos os dias' then
      begin
         a := DBEdit2.Text;
         b := DBEdit3.Text;
         c := DBRadioGroup1.Value;
         d := DBEdit5.Text;
         for i := 1 to 7 do
         begin
            DMEscola.TTarifasDiaSemana.Value := dias[i];
            DBEdit2.Text := a;
            DBEdit3.Text := b;
            DMEscola.TTarifasTarifaacada.Value := StrToTime(c);
            DBEdit5.Text := d;
            x := DBComboBox1.Text;
            horai := DBEdit2.Text;
            horaf := DBEdit3.Text;
            DMEscola.Qgravatarifa.Close;
            DMEscola.Qgravatarifa.SQL.Clear;
            DMEscola.Qgravatarifa.Sql.Add ('Select * From Tarifas');
            DMEscola.Qgravatarifa.Sql.Add('WHERE ');
            DMEscola.Qgravatarifa.Sql.Add('(:phora >= Horaini)  and');
            DMEscola.Qgravatarifa.Sql.Add('(:phora <= Horafim)  and');
            DMEscola.Qgravatarifa.Sql.Add('(DiaSemana = :pdiasemana) or');
            DMEscola.Qgravatarifa.Sql.Add('(:phoras <= Horafim)  and');
            DMEscola.Qgravatarifa.Sql.Add('(:phoras >= Horaini)  and');
            DMEscola.Qgravatarifa.Sql.Add('(DiaSemana = :pdiasemana)');
            DMEscola.Qgravatarifa.ParamByName('pdiasemana').AsString := x;
            DMEscola.Qgravatarifa.ParamByName('phora').AsTime := StrToTime(horai);
            DMEscola.Qgravatarifa.ParamByName('phoras').AsTime := StrToTime(horaf);
            DMEscola.Qgravatarifa.open;
            diaBusca:=DMEscola.QgravatarifaCobrar.AsString;
            if diabusca = '' then
            begin
               DMEscola.TTarifas.Post;
               DMEscola.TTarifas.Append
            end
            else
               ShowMessage('Esse intervalo não será possível, intervalo já cadastrado!');
         end;
      end
      else
      begin
         DMEscola.TTarifas.Post;
         btnExcluir.Enabled := true;
         SpeedButton1.enabled := true;
      end
   end
   else
   begin
      ShowMessage('Não será possível cadastrar esse intervalo de horário! Tente novamente!');
      DMEscola.TTarifas.Cancel
   end;
end;

procedure TFrmTarifas.btnExcluirClick(Sender: TObject);
begin
   DMEscola.TTarifas.Delete;
   if (DMEscola.TTarifas.EOF) then
      btnExcluir.Enabled := false;
      SpeedButton1.enabled := false;
end;

procedure TFrmTarifas.SpeedButton1Click(Sender: TObject);
begin
   DMEscola.TTarifas.Edit;
end;

procedure TFrmTarifas.btnRetornarClick(Sender: TObject);
begin
   close;
end;

procedure TFrmTarifas.SpeedButton3Click(Sender: TObject);
var
    x, diaBusca, hora, valorBusca, minutobusca : String;
    valor, valoracesso : real;
    tempomim : Integer;
begin
  hora := MaskEdit1.Text;
  tempomim := (StrToInt(Copy(hora,1,2))*60) + StrToInt(Copy(hora,4,2));
  Case dayofweek(Date) of
    1 :  x := 'Domingo';
    2 :  x := 'Segunda-Feira';
    3 :  x := 'Terça-Feira';
    4 :  x := 'Quarta-Feira';
    5 :  x := 'Quinta-Feira';
    6 :  x := 'Sexta-Feira';
    7 :  x := 'Sábado';
  end;
  DMEscola.Qtestetarifa.Close;
  DMEscola.Qtestetarifa.SQL.Clear;
  DMEscola.Qtestetarifa.Sql.Add ('Select * From Tarifas');
  DMEscola.Qtestetarifa.Sql.Add('WHERE ');
  DMEscola.Qtestetarifa.Sql.Add('(DiaSemana = :pdiasemana) and');
  DMEscola.Qtestetarifa.Sql.Add('(Horaini < :phora) and');
  DMEscola.Qtestetarifa.Sql.Add('(Horafim > :phoras)');
  DMEscola.Qtestetarifa.ParamByName('pdiasemana').AsString := x;
  DMEscola.Qtestetarifa.ParamByName('phora').AsTime := Time;
  DMEscola.Qtestetarifa.ParamByName('phoras').AsTime := Time;
  DMEscola.Qtestetarifa.open;
  diaBusca:=DMEscola.QtestetarifaDiaSemana.Value;
  valorBusca := DMEscola.QtestetarifaCobrar.Text;
  minutobusca := DMEscola.QtestetarifaTarifaacada.Text;

  If diaBusca <> '' then
  begin
     if minutobusca = '00:01:00' then
     begin
        valor := StrToFloat(valorbusca);
        valoracesso := valor * tempomim;
        label13.caption := FloatToStr(valoracesso);
     end
     else
     begin
        tempomim := tempomim div 2;
        valor := StrToFloat(valorbusca);
        valoracesso := valor * tempomim;
        label13.caption := FloatToStr(valoracesso);
     end
  end
  else
     Showmessage('Não existe tarifa para esse horário!'+#13+'Por favor cadastre!');

end;

procedure TFrmTarifas.btnAdicionarClick(Sender: TObject);
begin
   DMEscola.TTarifas.Append;
   btnExcluir.Enabled := true;
   SpeedButton1.enabled := true;
end;

procedure TFrmTarifas.SpeedButton2Click(Sender: TObject);
var
  valor, x, y : real;
begin
   valor := StrToFloat(Edit1.Text);
   x := Round((valor/60)*1000)/1000;
   y := Round((valor/30)*1000)/1000;
   label9.caption := FloatToStr(x);
   label10.caption := FloatToStr(y);

end;

procedure TFrmTarifas.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if Key = Vk_F12 then
      close;
end;

end.
